﻿using System;
using System.Collections.Generic;
using SGRapidForm.Common;
using SGRapidForm.Permission.Service.Interface;
using SGRapidForm.Permission.Entity;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;

namespace SGDemo.Permission
{
    public partial class AssociatedPagesAndButtons : System.Web.UI.Page
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        protected void Page_Load(object sender, EventArgs e)
        {
            JSManager.RegAjax(Header, this.GetType().BaseType);
            if (!this.IsPostBack)
            {
                Bind();
            }
        }

        protected void Bind()
        {
            if (this.Request.QueryString["Keyid"] == null)
            {
                throw new ArgumentException("查询字符串‘Keyid’不能为空");
            }
            List<PermissionDetails> PerDetailsCollection = _permissionService.RoleModule_GetDetailsByRoleKeyid(this.Request.QueryString["Keyid"]);

            cbModule.Items.Clear();
            //添加第一项，默认为选中此项
            ListEditItem all = new ListEditItem("未选择", "none");
            all.Selected = true;
            cbModule.Items.Add(all);
            //添加拥有权限的模块
            ListEditItem lei;
            foreach (PermissionDetails item in PerDetailsCollection)
            {
                if (item.IsSelected)
                {
                    lei = new ListEditItem(item.Name, item.Keyid);
                    cbModule.Items.Add(lei);
                }

            }

        }

        /// <summary>
        /// 自定义GridView回调重新绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwList_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            gvwPageList.PageIndex = 0;
            gvwPageList.DataBind();
        }

        protected void gvwList_CustomCallback_Button(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            gvwButtonList.DataBind();
        }

        [AjaxPro.AjaxMethod]
        public bool CheckedChangedPage(string roleKeyid, string pageKeyid, bool isSelected)
        {
            if (isSelected)
            {
                return _permissionService.RolePage_Add(roleKeyid, pageKeyid);
            }
            else
            {
                return _permissionService.RolePage_Delete(roleKeyid, pageKeyid);
            }
        }

        [AjaxPro.AjaxMethod]
        public bool CheckedChangedButton(string roleKeyid, string buttonKeyid, bool isSelected)
        {
            if (isSelected)
            {
                return _permissionService.RoleButton_Add(roleKeyid, buttonKeyid);
            }
            else
            {
                return _permissionService.RoleButton_Delete(roleKeyid, buttonKeyid);
            }
        }
    }

    public class PagesAndButtonsDataSource
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        public List<PermissionDetails> GetDetailsByRoleKeyidAndModuleKeyid(string roleKeyid, string filterConditions)
        {
            return _permissionService.RolePage_GetDetailsByRoleKeyidAndModuleKeyid(roleKeyid, filterConditions);
        }

        public List<PermissionDetails> GetDetailsByRoleKeyidAndPageKeyid(string roleKeyid, string pageKeyid)
        {
            return _permissionService.RoleButton_GetDetailsByRoleKeyidAndPageKeyid(roleKeyid, pageKeyid);
        }
    }
}